# -*- coding: utf-8 -*-

import wx
import wx.xrc
from gui.hint_dialogs import ErrorDialog
from gui.validators.validator import Validator
from config.config import config
from infrastructure.constant.response_constant import ResponseConstant
from infrastructure.utils.result_wrap import ResponseWrapper


class CloudSettingDialog(wx.Dialog):

    def __init__(self, parent):
        wx.Dialog.__init__(self, parent, id=wx.ID_ANY, title=f"{parent.current_cloud.name} setting".upper(), pos=wx.DefaultPosition,
                           size=wx.Size( 300,50 ), style=wx.DEFAULT_DIALOG_STYLE)
        self.SetSizeHints( wx.Size( 200,50 ), wx.Size( 1000,1000 ))
        self.setting_item_dict = {}

        bSizer = wx.BoxSizer(wx.VERTICAL)

        bSizer_center_ver = wx.BoxSizer(wx.VERTICAL)
        if parent.current_cloud is not None:
            label_width = 50
            for form in parent.current_cloud.setup_items.values():
                bSizer_item = wx.BoxSizer(wx.HORIZONTAL)

                # __init__(self, parent=None, id=None, label=None, pos=None, size=None, style=0, name=None)
                staticText_label = wx.StaticText(self, wx.ID_ANY, form.label, wx.Point(-1, -1), wx.Size(100, -1), wx.ALIGN_RIGHT)
                staticText_label.Wrap(-1)
                bSizer_item.Add(staticText_label, 0, wx.ALIGN_RIGHT | wx.ALL, 5)
                # label_width = label_width if staticText_label.Size[0] <= label_width else staticText_label.Size[0]
                self.setting_item_dict[form.name] = config.get(parent.current_cloud.name, form.name)
                # __init__(self, parent=None, id=None, value=None, pos=None, size=None, style=0, validator=None, name=None)
                m_textCtrl101 = wx.TextCtrl(self, wx.ID_ANY, self.setting_item_dict[form.name], wx.DefaultPosition, wx.DefaultSize, wx.TE_LEFT, validator=Validator(validator=form.validator), name=form.name)
                m_textCtrl101.SetMinSize(wx.Size(200, -1))
                m_textCtrl101.SetMaxSize(wx.Size(1000, -1))
                bSizer_item.Add(m_textCtrl101, 0, wx.ALIGN_LEFT | wx.ALL | wx.EXPAND, 5)
                bSizer_center_ver.Add(bSizer_item, 1, wx.EXPAND, 5)
        bSizer.Add(bSizer_center_ver, 1, wx.EXPAND | wx.TOP, 20)
        bSizer8 = wx.BoxSizer(wx.HORIZONTAL)
        self.b_cancel = wx.Button(self, wx.ID_ANY, u"取消", wx.DefaultPosition, wx.DefaultSize, 0)
        bSizer8.Add(self.b_cancel, 0, wx.ALL, 5)
        bSizer8.Add((30, 0), 1, wx.EXPAND, 5)
        self.b_submit = wx.Button(self, wx.ID_ANY, u"提交", wx.DefaultPosition, wx.DefaultSize, 0)
        bSizer8.Add(self.b_submit, 0, wx.ALL, 5)

        bSizer.Add( bSizer8, 1, wx.ALIGN_CENTER|wx.TOP, 20 )

        self.SetSizer(bSizer)
        self.Layout()
        bSizer.Fit(self)

        self.Centre(wx.BOTH)

        self.Bind(wx.EVT_CLOSE, self.on_close)
        self.b_cancel.Bind(wx.EVT_BUTTON, self.on_close)
        self.b_submit.Bind(wx.EVT_BUTTON, self.submit)

    def __del__(self):
        pass

    def on_close(self, event):
        self.Show(False)

    def submit(self, event):
        flag = True
        for key in list(self.setting_item_dict.keys()):
            item = self.FindWindowByName(key)
            validator = item.Validator
            if validator is not None:
                result: ResponseWrapper = validator.Validate(self)
                if result.code == ResponseConstant.FAIL:
                    ErrorDialog(self, f"字段【{self.Parent.current_cloud.setup_items[key].label}】值：{result.msg if result.msg is not None else ''}").Show()
                    flag = False
                    break
            self.setting_item_dict[key] = item.Value
        if flag:
            self.Parent.set_cloud(self.setting_item_dict)
            self.Show(False)